﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Deliveryroomstatusbulletin.BusinessRules;
using Deliveryroomstatusbulletin.EntityData;
using DeliveryRoomStatusBulletin.Properties;
using SystemFramework.NewCommon;
using System.Threading;
using System.Windows.Media.Animation;

namespace DeliveryRoomStatusBulletin
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        //刷新间隔 单位 S
        private int refreshPeroid;
        public MainWindow()
        {
            InitializeComponent();
            ReadConfig();
          
            
        }

        private void ReadConfig() 
        {
            tbHosName.Text = Settings.Default.HospitalName;
            qrcode.Text = Settings.Default.QRCode;
            refreshPeroid = Settings.Default.RefreshPeriod;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //media.Play();
            //media.Position = TimeSpan.Zero;
            //加载在产孕妇数据
            BindList();
            //时间显示Timer
            DispatcherTimer dt_Time = new DispatcherTimer();
            dt_Time.Interval = TimeSpan.FromSeconds(1);
            dt_Time.Tick += new EventHandler(dt_Time_Tick);//调用函数
            dt_Time.Start();

            //刷新数据
            DispatcherTimer dtRefresh = new DispatcherTimer();
            dtRefresh.Interval = TimeSpan.FromSeconds(refreshPeroid);
            dtRefresh.Tick += new EventHandler(dtRefresh_Tick);
            dtRefresh.Start();

            //切屏
            DispatcherTimer dt_TabWin = new DispatcherTimer();
            dt_TabWin.Interval = TimeSpan.FromSeconds(10);
            dt_TabWin.Tick += new EventHandler(dt_TabWin_Tick);
            dt_TabWin.Start();

            //DispatcherTimer dt_BindWaits = new DispatcherTimer();
            //dt_BindWaits.Interval = TimeSpan.FromSeconds(10);
            //dt_BindWaits.Tick += new EventHandler(BindWaits);
            //dt_BindWaits.Start();

            textBlock.Text = string.Empty;
       
        }

        private void BindWaits(object sender, EventArgs e) 
        {
            List<string> list = GetWaitsList();
            foreach (string dis in list)
            {
                textBlock.Text = dis;
                Thread.Sleep(1000);
            }
        }

        #region 显示时间Timer

        void dt_Time_Tick(object sender, EventArgs e)
        {
            this.tbDate.Text = DateTime.Now.Date.ToString("yyyy/MM/dd");
            this.tbTime.Text = DateTime.Now.ToString("HH:mm:ss");
        }

        #endregion

        #region 刷新数据，待产、在产

        void dtRefresh_Tick(object sender, EventArgs e)
        {
            BindList();
            Alert();
        }

        #endregion

        #region 切换窗体

        int flag = 0;
        void dt_TabWin_Tick(object sender, EventArgs e)
        {
            Storyboard sb;
            if (flag == 0)
            {
                sb = this.TryFindResource("ShowContent") as Storyboard;
                flag = 1;
            }
            else
            {
                sb = this.TryFindResource("ShowTag") as Storyboard;
                flag = 0;
            }
            sb.Begin();
        }

        #endregion

        #region 加载在产孕妇数据

        private void BindList() 
        {
            DeliveryroomstatusbulletinRules deliRule = new DeliveryroomstatusbulletinRules();
            EntityList<DeliveryroomstatusbulletinData> listDeliData = deliRule.GetAll();
            listboxdata.ItemsSource = listDeliData;
        }

        #endregion

        #region 遍历在产孕妇信息，并提醒所需物品

        private void Alert() 
        {
            List<DeliveryroomstatusbulletinData> lstr = new List<DeliveryroomstatusbulletinData>();
            DeliveryroomstatusbulletinRules deliRule = new DeliveryroomstatusbulletinRules();
            EntityList<DeliveryroomstatusbulletinData> listDeliData = deliRule.GetAll();
            foreach (DeliveryroomstatusbulletinData item in listDeliData)
            {
                if (item.Alertstatus == 0 && !string.IsNullOrEmpty(item.Goods)) 
                {
                    lstr.Add(item);
                }
            }

            if (lstr.Count < 1)
            {
                return;
            }
            for (int i = 0; i < lstr.Count; i++)
            {
                RemindWindow rw = new RemindWindow(lstr[i]);
                rw.ShowDialog();
                lstr[i].Alertstatus = 1;
                deliRule.Update(lstr[i]);
                Thread.Sleep(10000);
            }
        }

        #endregion

        #region 保证内容修改后加载显示文字动画

        DispatcherTimer dt2 = new DispatcherTimer();
        private void textBlock_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            
            dt2.Interval = TimeSpan.FromSeconds(.6);
            dt2.Tick += new EventHandler(dt2_Tick);
            dt2.Start();
            Storyboard sb = this.TryFindResource("ShowText") as Storyboard;
            sb.Begin();
            
        }
        void dt2_Tick(object sender, EventArgs e)
        {
            textBlock.Text = textBlock.DataContext.ToString();
            dt2.Stop();
        }

        #endregion

        #region 获取并绑定待产孕妇的床号，姓名添加至5人一组的集合
        private List<string> GetWaitsList() 
        {
            List<string> lstr = new List<string>();
            string strVal = "";
            DeliveryroomstatusbulletinRules deliRule = new DeliveryroomstatusbulletinRules();
            List<DeliveryroomstatusbulletinData> list = deliRule.GetWaits();
            for (int i = 0; i < list.Count; i++) 
            {
                if ((i+1) % 5 != 0)
                {
                    strVal = strVal + list[i].Bedid + " " + list[i].Name + "      ";
                }
                else 
                {
                    strVal = string.Empty;
                    strVal = strVal + list[i].Bedid + " " + list[i].Name + "      ";
                }
                lstr.Add(strVal);
            }
            return lstr;
        }

        #endregion
    }
}
